package com.bw.xiaosy.shiro;



import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;


/**
 * @author Xiaosy
 * @date 2017-03-20 17:29
 */
public class ShiroUtil {

    /**
     * 判断是否需要认证
     * @param bean
     * @return true 不需要  false 需要
     */
    public static boolean isContains(NotAuthorizationBean bean)
    {
        List<String> paths = bean.getPaths();
        String name = bean.getName();
        for(String path : paths)
        {
            if(path.equalsIgnoreCase(name))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 统一返回前端json数据
     * @param response
     * @param data
     */
    public static void writeResponse(HttpServletResponse response, Object data)
    {
        try {
            response.setContentType("application/json");
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(JSON.toJSONString(data).getBytes("UTF-8"));

            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
